package com.example.ruoyivoicecall;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static com.example.ruoyivoicecall.common.VoiceUnitUtil.ConvertStreamToString;

public class T2 {


    public static void main(String[] args) {
        // 定义登录所需变量
        String sysId = "1761526784453";
        String idArea = "10010101";
        String username = "1000";
        String password = "1";
        String voId = "login";
        String serverUrl ="http://192.168.100.12:7627";

        // 创建HttpClient实例
        try (org.apache.hc.client5.http.impl.classic.CloseableHttpClient httpClient =
                     org.apache.hc.client5.http.impl.classic.HttpClients.createDefault()) {

            // 创建POST请求
            HttpPost httpPost = new HttpPost(serverUrl+"/v/logon");

            // 设置请求头
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            // 构建请求参数
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("sysId", sysId));
            params.add(new BasicNameValuePair("id_area", idArea));
            params.add(new BasicNameValuePair("j_username", username));
            params.add(new BasicNameValuePair("j_password", password));
            params.add(new BasicNameValuePair("vo_id", voId));

            // 设置请求实体
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            // 执行请求
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                // 检查状态码
                if (response.getCode() != 200) {
                    System.err.println("登录请求失败，状态码：" + response.getCode());
                    return;
                }

                // 读取响应内容
                HttpEntity entity = response.getEntity();
                String responseBody;
                if (entity != null) {
                    try (InputStream inputStream = entity.getContent()) {
                        responseBody = new String(inputStream.readAllBytes());
                    }
                } else {
                    responseBody = "";
                }

                // 判断登录是否成功
                if (responseBody.contains("frameset")) {
                    System.out.println("登录成功！");
                    // 输出当前httpClient的变量信息（此处为示例，实际可根据需要输出具体信息）
                    System.out.println("当前登录成功状态下的httpClient实例：" + httpClient);
                    //复制语音文件到指定目录
                    String pcmFilePath="D:\\output_pcm-dev-test.wav";
                    java.io.File srcFile = new java.io.File(pcmFilePath);
                    java.io.File destDir = new java.io.File("D:\\it9000\\lcall4\\v\\voice");
                    if (!destDir.exists()) {
                        System.err.println("目标文件夹不存在：" + destDir.getAbsolutePath() + "，停止执行。");
                        return;
                    }
                    java.io.File destFile = new java.io.File(destDir, srcFile.getName());
                    try {
                        java.nio.file.Files.copy(srcFile.toPath(), destFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
                        System.out.println("语音文件已复制到：" + destFile.getAbsolutePath());
                    } catch (IOException e) {
                        System.err.println("复制文件失败：" + e.getMessage());
                        return;
                    }
                    //开始刷新语音文件
//                    HttpGet httpget = new HttpGet(serverUrl + "/v/vack?vo_id=refreshVoiceList&vtype=BPara&vflag=refresh&repType=ackjsp&ackjsp=/jsp/v_voice.jsp");
                    HttpGet httpget = new HttpGet(serverUrl + "/v/vack?vo_id=reloadAllIndexString&vtype=V&repType=xml&_idaction_=21&segNo=0&ifAjax=false");
                    CloseableHttpResponse refresh_response = httpClient.execute(httpget);
                    try {
                        HttpEntity ref_entity = refresh_response.getEntity();
                        InputStream instreams = ref_entity.getContent();
                        String str = ConvertStreamToString(instreams);
                        System.out.println("刷新语音列表结果: " + str);
                    } finally {
                        response.close();
                    }
                } else {
                    System.err.println("登录失败，响应内容不包含frameset。");
                    return;
                }
            } catch (IOException e) {
                System.err.println("请求执行失败：" + e.getMessage());
                return;
            }
        } catch (IOException e) {
            System.err.println("HttpClient创建失败：" + e.getMessage());
        }

    }
}
